﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using DependencyInjectionHelperLib.Attributes;
using DependencyInjectionHelperLib.Navigation;
using MeterReader.Messages;
using MeterReader.Models;
using MeterReader.Services;
using System.Collections.ObjectModel;

namespace MeterReader.ViewModels
{
    [DIRegistration(pageKey: "MetersCurve")]
    public partial class MetersCurveViewModel : ObservableObject
    {
        private INavigationObjectProvider? _navigationObjectProvider;
        private IDataService? _readService;

        public MetersCurveViewModel()
        { }

        public MetersCurveViewModel(INavigationObjectProvider navigationObjectProvider, IDataService readService)
        {
            _navigationObjectProvider = navigationObjectProvider;
            _readService = readService;
            MeterDataCollections = _readService.GetMeterDataCollections();
        }

        public ObservableCollection<MeterDataCollection> MeterDataCollections { get; set; } = new();

        [ObservableProperty]
        private bool? _isPopupOpen = false;

        [ObservableProperty]
        private MeterDataCollection? _popupMeterDatas;

        [RelayCommand]
        private void GoBackDataMonitorView(string key)
        {
            var vm = _navigationObjectProvider!.GetNavigationObject<MetersDataMonitorViewModel>(key);
            _ = WeakReferenceMessenger.Default.Send(new NavigateRequestMessage(vm!));
        }

        [RelayCommand]
        private void OpenPopup(object obj)
        {
            var meterDataCollection = obj as MeterDataCollection;
            if (meterDataCollection != null)
            {
                PopupMeterDatas = meterDataCollection;
                IsPopupOpen = true;
            };
        }
    }
}